package com.order.controller;

import com.common.VO.SysResult;
import com.common.domain.Order;
import com.order.exception.MsgException;
import com.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Elias
 * @version 1.0
 * @date 2021-06-25 10:40
 * @description
 * @copyright 2021
 */
@RestController
@RequestMapping("/order")
public class OrderController {
  @Autowired private OrderService orderService;

  @RequestMapping("/manage/save")
  @ResponseBody
  public SysResult addOrder(@RequestBody Order order) {
    try {
      orderService.addOrder(order);
      return SysResult.ok();
    } catch (MsgException e) {
      e.printStackTrace();
      return SysResult.build(201, e.getMessage(), e);
    } catch (Exception e) {
      e.printStackTrace();
      return SysResult.build(500, e.getMessage(), e);
    }
  }

  @RequestMapping("/manage/query/{userId}")
  @ResponseBody
  public List<Order> orderQuery(@PathVariable("userId") String userId) {
    return orderService.queryOrder(userId);
  }

  @RequestMapping("/manage/delete/{orderId}")
  @ResponseBody
  public SysResult deleteOrder(@PathVariable("orderId") String orderId) {
    try {
      orderService.deleteOrder(orderId);
      return SysResult.ok();
    } catch (MsgException e) {
      e.printStackTrace();
      return SysResult.build(201, e.getMessage(), e);
    } catch (Exception e) {
      e.printStackTrace();
      return SysResult.build(500, e.getMessage(), e);
    }
  }
}
